//
//  CoreDataManager.m
//  Lesson5_Notes 记事本
//
//  Created by xalo on 16/5/31.
//  Copyright © 2016年 朱旭东. All rights reserved.
//
#import "CoreDataManager.h"

@interface CoreDataManager ()
@property (nonatomic,strong) NSManagedObjectContext *context;


@end
@implementation CoreDataManager

static CoreDataManager *handle = nil;

//懒加载
- (NSManagedObjectContext *)context
{
    if (!_context) {
        _context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    }
    return _context;
}

+ (instancetype)defaultManager{
    if (!handle) {
        handle = [[CoreDataManager alloc]init];
    }
    return handle;
}
//初始化
- (instancetype)init
{
    self = [super init];
    if (self) {
        //创建数据模型
        NSManagedObjectModel  *mode = [[NSManagedObjectModel alloc] initWithContentsOfURL:[[NSBundle  mainBundle] URLForResource:@"CollectionModel" withExtension:@"momd"]];
        //创建持久化助理
        NSPersistentStoreCoordinator *stor = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mode];
        //关联路径
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject  stringByAppendingPathComponent:@"reade.sqlite"];
        NSLog(@"%@",filePath);
        //设置初始化助理属性
        [stor   addPersistentStoreWithType:NSSQLiteStoreType configuration:nil  URL:[NSURL  fileURLWithPath:filePath] options:nil error:nil];
        //模型关联初始化助理
        self.context.persistentStoreCoordinator  = stor;
    }
    return self;
}


//查询整个收藏
- (NSArray *)selectAllCollectionModel{
    
    NSFetchRequest  *request = [NSFetchRequest  fetchRequestWithEntityName:@"CollectionModel"];
    NSArray *array = [self.context  executeFetchRequest:request error:nil];
    return array;
}

//看有没有已经存在的阅读收藏
- (BOOL) isContainsReadWithReadID:(NSNumber *)readID{
    NSFetchRequest *request = [NSFetchRequest  fetchRequestWithEntityName:@"CollectionModel"];
    request.predicate = [NSPredicate  predicateWithFormat:@"readID = %@",readID];
    NSArray *arrray = [_context  executeFetchRequest:request error:nil];
    if (arrray.count  !=  0 ) {
        return YES;
    }else{
        return NO;
    }
}
//根据readID去删除一个收藏
-(void)deleteCollectionModelWithReadID:(NSNumber *)readID{
    NSFetchRequest *requet =[NSFetchRequest  fetchRequestWithEntityName:@"CollectionModel"];
    requet.predicate = [NSPredicate  predicateWithFormat:@"readID = %@",readID];
     CollectionModel *model     = [self.context  executeFetchRequest:requet error:nil].lastObject;
    [self.context deleteObject:model];
    [self.context  save:nil];
    }


//插入
- (void)insertCollectionMmodelWothModel:(ReadListModel *)model  html:(NSString *)html{
    
    CollectionModel *collectioModel = [NSEntityDescription insertNewObjectForEntityForName:@"CollectionModel" inManagedObjectContext:self.context];
    
    collectioModel.title = model.title;
    collectioModel.readID = [NSString stringWithFormat:@"%@",model.ID];
    collectioModel.coverimg = model.coverimg;
    collectioModel.content = model.content;
    collectioModel.html = html;
//    [self.context  save:nil];
    
    if ([self isContainsReadWithReadID:model.ID]) {
        
        //表示收藏成功
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        
        [alertView show];
        return ;
    }
    
}


@end
